/**
 * 
 */
package com.platform.template.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;


/**
 * <pre>
 * 功能：Jdbc操作工具类
 * 
 * &lt;bean id="myJdbcTemplate" class="com.platform.template.jdbc.MyJdbcTemplate"&gt;
 * 		&lt;property name="jdbcUrl" value="${jdbc.jdbcUrl}" /&gt;
 * 		&lt;property name="username" value="${jdbc.username}" /&gt;
 * 		&lt;property name="password" value="${jdbc.password}" /&gt;
 * 		&lt;property name="driverClass" value="${jdbc.driverClass}" /&gt;
 * &lt;/bean&gt;
 * 
 * 在properties文件中配置：
 * jdbc.jdbcUrl
 * jdbc.username
 * jdbc.password
 * jdbc.driverClass
 * </pre>
 * @author wei.gao
 */
public class MyJdbcTemplate {
	private String driverClass = "com.mysql.jdbc.Driver";
	private String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
	private String username = "root";
	private String password = "123456";
	private int initialSize = 100;
	private int maxActive = 50;
	private int minIdle = 100;
	private int maxWait = 10000;
	
	// 
	private DruidDataSource dataSource = null;
	
	/**
	 * <pre>
	 * 功能：获取数据源
	 * </pre>
	 * @return
	 */
	public DruidDataSource getDataSource(){
		if(dataSource == null){
			synchronized (MyJdbcTemplate.class) {
				if(dataSource == null){
					dataSource = new DruidDataSource();
					dataSource.setDriverClassName(driverClass);
					dataSource.setUrl(jdbcUrl);
					dataSource.setUsername(username);
					dataSource.setPassword(password);
					dataSource.setInitialSize(initialSize);
					dataSource.setMaxActive(maxActive);
					dataSource.setMinIdle(minIdle);
					dataSource.setMaxWait(maxWait);
					dataSource.setTimeBetweenEvictionRunsMillis(60000);
					dataSource.setMinEvictableIdleTimeMillis(300000);
					dataSource.setPoolPreparedStatements(true);
					dataSource.setMaxOpenPreparedStatements(100);
					dataSource.setTestOnBorrow(false);
					dataSource.setTestWhileIdle(true);
				}
			}
		}
		return dataSource;
	}
	
	// 
	private JdbcTemplate template = null;
	
	/**
	 * <pre>
	 * 功能：获取数据库操作对象
	 * </pre>
	 * @return
	 */
	public JdbcTemplate getTemplate(){
		if(template == null){
			synchronized (MyJdbcTemplate.class) {
				if(template == null){
					template = new JdbcTemplate(getDataSource());
				}
			}
		}
		return template;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getInitialSize() {
		return initialSize;
	}

	public void setInitialSize(int initialSize) {
		this.initialSize = initialSize;
	}

	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public int getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}
}
